//
//  PrescriptionOrderReceiveCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/3/3.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class PrescriptionOrderReceiveCell: UITableViewCell {
    
    var handlerClosure: ((PrescriptionOrderDetailCellViewModel) -> ())?
    
    private var viewModel: PrescriptionOrderDetailCellViewModel?

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .clear
        contentView.backgroundColor = .clear
    }
    
    func bind(to viewModel: PrescriptionOrderDetailCellViewModel) {
        self.viewModel = viewModel
        tipLabel.isHidden = viewModel.receiveType != .express
        tipLabel.text = viewModel.emsFee
        
        setupButton(with: viewModel)
    }
    
    private func setupButton(with viewModel: PrescriptionOrderDetailCellViewModel) {
        expressButton.isSelected = viewModel.receiveType == .express
        storeButton.isSelected = viewModel.receiveType == .drugstore
        hospitalButton.isSelected = viewModel.receiveType == .hospital
    }
    
    @IBOutlet private weak var tipLabel: UILabel!
    
    @IBOutlet private weak var expressButton: UIButton!
    @IBOutlet private weak var storeButton: UIButton! {
        didSet {
            storeButton.isHidden = true
        }
    }
    @IBOutlet private weak var hospitalButton: UIButton!
    
    @IBAction private func buttonClicked(_ sender: UIButton) {
        guard let viewModel = viewModel else { return }
        
        guard !sender.isSelected else { return }
        
        if sender == expressButton {
            viewModel.receiveType = .express
        } else if sender == storeButton {
            viewModel.receiveType = .drugstore
        } else if sender == hospitalButton {
            viewModel.receiveType = .hospital
        }
        
        setupButton(with: viewModel)
        
        handlerClosure?(viewModel)
    }
}
